﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormsDemo1
{
    public partial class AddUpdateBookForm : Form
    {
        public delegate void RefreshDelegate();
        public event RefreshDelegate refresh;
        public static int tag = 0;

        public AddUpdateBookForm()
        {
            InitializeComponent();
            if (tag == 1) 
            {
                //设置书号为只读
                textBox1.ReadOnly = true;
            }
            if (tag == 0) 
            {
                //把书号为只读移除 防止添加时输入不了书号
                textBox1.ReadOnly = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DatabaseData dbd = new DatabaseData();
            //tag为0为添加操作
            if (tag == 0) 
            {
                string sql = $"INSERT INTO dbo.book VALUES ('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}'," +
                $"{Convert.ToInt32(textBox4.Text)},'{textBox5.Text}')";
                int result = dbd.Execute(sql);
                if (result > 0)
                {
                    MessageBox.Show("操作成功");
                }
                else
                {
                    MessageBox.Show("操作失败");
                }
                clearTextBox();
                this.Close();
                this.refresh();
            }
            //tag==1为修改操作
            if (tag == 1) {
                //先查询数据库是否存在此书号
                string sql =$"SELECT ID FROM book WHERE ID = {textBox1.Text}";
                IDataReader reader = dbd.read(sql);
                //如果查询不到数据 则不让修改
                if (reader.Read())
                {
                    var updateSql = $"UPDATE book SET id = '{textBox1.Text}',name='{textBox2.Text}'," +
                        $"author='{textBox3.Text}',number='{Convert.ToInt32(textBox4.Text)}',press='{textBox5.Text}' WHERE id ='{textBox1.Text}'";
                    int res = dbd.Execute(updateSql);
                    if (res > 0)
                    {
                        MessageBox.Show("操作成功");
                        clearTextBox();
                        this.Close();
                        this.refresh();
                        return;
                    }
                    MessageBox.Show("操作失败");
                }
                else 
                {
                    MessageBox.Show("此书籍不存在，不允许修改");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clearTextBox();
        }

        public void bindData(string id, string name, string author, string number, string press) 
        {
            textBox1.Text = id;
            textBox2.Text = name;
            textBox3.Text = author;
            textBox4.Text = number;
            textBox5.Text = press;
        }
        //清空文本控件的值
        public void clearTextBox() {
            foreach (var item in this.Controls)
            {
                if (item is TextBox)
                {
                    (item as TextBox).Text = string.Empty;
                }
            }
        }
    }
}
